/*
 * Copyright (c) 2010 Scott Morgan
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package captaintest;

import captaintest.util.Assert;

import java.text.DecimalFormat;

public class Coverage {

    private String type;
    private Double bestCoveredLines;
    private Integer bestTotalLines;
    private Double bestPercentCovered;

    private Double lastCoveredLines;
    private Integer lastTotalLines;
    private Double lastPercentCovered;

    private Boolean error;

    public Coverage(String type, double bestCoveredLines, int bestTotalLines) {
        this.type = type;
        this.bestCoveredLines = bestCoveredLines;
        this.bestTotalLines = bestTotalLines;
        this.bestPercentCovered = round(bestCoveredLines / bestTotalLines);
    }

    public Coverage(String type, double bestPercentCovered) {
        this.type = type;
        this.bestPercentCovered = round(bestPercentCovered);
    }

    public String getType() {
        return type;
    }

    public boolean hasError() {
        return Boolean.TRUE.equals(error);
    }

    private double round(double d) {
        DecimalFormat format = new DecimalFormat("#.######");
        return Double.valueOf(format.format(d));
    }


    public Double getBestCoveredLines() {
        return bestCoveredLines;
    }

    public Integer getBestTotalLines() {
        return bestTotalLines;
    }

    public double getBestPercentCovered() {
        return bestPercentCovered;
    }

    public Double getLastPercentCovered() {
        return lastPercentCovered == null ? 0 : lastPercentCovered;
    }

    public Double getDiff(){
        if (lastPercentCovered == null){
            return -1 * bestPercentCovered;
        } else if (bestPercentCovered == null){
            return lastPercentCovered;
        } else{
            return lastPercentCovered - bestPercentCovered;
        }
    }

    public void mergeCoverage(Coverage lastCoverage){
        Assert.isTrue(this.type.equals(lastCoverage.getType()));

        if (lastCoverage.getBestPercentCovered() < getBestPercentCovered()){
            this.error = true;
            this.lastCoveredLines = lastCoverage.getBestCoveredLines();
            this.lastTotalLines = lastCoverage.getBestTotalLines();
            this.lastPercentCovered = lastCoverage.getBestPercentCovered();
        } else {
            this.bestCoveredLines = lastCoverage.getBestCoveredLines();
            this.bestTotalLines = lastCoverage.getBestTotalLines();
            this.bestPercentCovered = lastCoverage.getBestPercentCovered();
            resetError();
        }
    }

    private void resetError() {
        this.error = null;
        this.lastCoveredLines = null;
        this.lastTotalLines = null;
        this.lastPercentCovered = null;
    }
}
